const mongoose = require('mongoose');

const itemSchema = new mongoose.Schema({
    title: {
        type: String,
        required: [true, '物品名称是必需的'],
        trim: true
    },
    images: {
        type: [String],
        required: [true, '物品图片是必需的'],
        validate: {
            validator: function (v) {
                return v && v.length > 0;
            },
            message: '至少需要一个图片'
        }
    },
    tags: {
        type: [String],
        required: [true, '物品标签是必需的'],
        validate: {
            validator: function (v) {
                return v && v.length > 0;
            },
            message: '至少需要一个标签'
        }
    },
    status: {
        type: String,
        required: [true, '物品状态是必需的'],
        enum: ['在库', '已借出', '已损坏', '已丢失'],
        default: '在库'
    },
    location: {
        type: String,
        required: [true, '物品位置是必需的'],
        trim: true
    },
    description: {
        type: String,
        default: ''
    },
    addedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    }
}, {
    timestamps: true
});

// 添加文本索引以支持搜索
itemSchema.index({ title: 'text', description: 'text', location: 'text', tags: 'text' });

const Item = mongoose.model('Item', itemSchema);

module.exports = Item; 